#include "texture.h"
#include <iostream>

Texture::Texture(const char *filename):
    image(filename), texID(0)
{
    image.AddAlpha(0.0);
    PrepareTex();
}

void Texture::PrepareTex()
{
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    GLuint texBuffer = 0;
    glGenBuffers(1, &texBuffer);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texBuffer);
    glBufferData(GL_PIXEL_UNPACK_BUFFER, image.DataSize(), image.Data(), GL_STATIC_DRAW);

    glGenTextures(1, &texID);
    glBindTexture(GL_TEXTURE_2D, texID);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.Width(), image.Height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
}

Texture::~Texture()
{
    //glDeleteTextures(1, &texID);
}
